/*
 * Copyright (c) 2019. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package org.paul.tourism

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager

/**
 *
 * 网络状态监听广播接收器，当网络状态发生变化的时候，该接收器会收到来自系统的广播.
 *
 * created by paul at 2019-03-27
 */

class ActiveReceiver : BroadcastReceiver(), Logger {
    override fun onReceive(context: Context?, intent: Intent?) {

        context?.let {
            fmtd(context.getString(R.string.netchange_broadcast_fmt), intent?.let { toString() }
                    ?: context.getString(R.string.netchange_broadcast_empty))
            val connective = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val networkInfo = connective.activeNetworkInfo
            fmtd(context.getString(R.string.network_info_fmt), networkInfo?.let { toString() }
                    ?: context.getString(R.string.network_info_empty))
            networkInfo?.let { info ->
                Tourism.instance().broadcast(info.isConnected)
            } ?: d(context.getString(R.string.network_info_empty)).also {
                Tourism.instance().broadcast(connected=false)
            }
        }

    }

}